package com.ruoyi.purchase.controller;

import com.ruoyi.purchase.domain.FirmSupplier;
import com.ruoyi.purchase.service.IFirmSupplierService;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 供应商Controller
 *
 * @author ruoyi
 * @date 2023-07-16
 */
@RestController
@RequestMapping("/supplier")
public class FirmSupplierController extends BaseController
{
    @Autowired
    private IFirmSupplierService firmSupplierService;

    /**
     * 查询供应商列表
     */
    @RequiresPermissions("purchase:supplier:list")
    @GetMapping("/list")
    public TableDataInfo list(FirmSupplier firmSupplier)
    {
        startPage();
        List<FirmSupplier> list = firmSupplierService.selectFirmSupplierList(firmSupplier);
        return getDataTable(list);
    }

    /**
     * 导出供应商列表
     */
    @RequiresPermissions("purchase:supplier:export")
    @Log(title = "供应商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FirmSupplier firmSupplier)
    {
        List<FirmSupplier> list = firmSupplierService.selectFirmSupplierList(firmSupplier);
        ExcelUtil<FirmSupplier> util = new ExcelUtil<FirmSupplier>(FirmSupplier.class);
        util.exportExcel(response, list, "供应商数据");
    }

    /**
     * 获取供应商详细信息
     */
    @RequiresPermissions("purchase:supplier:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(firmSupplierService.selectFirmSupplierById(id));
    }

    /**
     * 新增供应商
     */
    @RequiresPermissions("purchase:supplier:add")
    @Log(title = "供应商", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FirmSupplier firmSupplier)
    {
        return toAjax(firmSupplierService.insertFirmSupplier(firmSupplier));
    }

    /**
     * 修改供应商
     */
    @RequiresPermissions("purchase:supplier:edit")
    @Log(title = "供应商", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FirmSupplier firmSupplier)
    {
        return toAjax(firmSupplierService.updateFirmSupplier(firmSupplier));
    }

    /**
     * 删除供应商
     */
    @RequiresPermissions("purchase:supplier:remove")
    @Log(title = "供应商", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(firmSupplierService.deleteFirmSupplierByIds(ids));
    }
}
